// vp_cai/pages/index/areas.js
var app = getApp();
var vp = require('../../resource/js/vp.js');
Page({
  data: {
	  drinking:'',
	  smoking:'',
    healthData: {
      family: [],
      personal: {
        drinking: '偶尔饮酒',
        smoking: '不吸烟'
      },
      past: {
        diseases: '肺炎(2018年)',
        surgeries: '阑尾切除术(2015年)',
        allergies: '青霉素过敏',
        medications: '长期服用降压药'
      }
    }
  },

  onLoad: function(options) {
	  const that = this
    // 可以在这里从服务器加载健康史数据
	const eventChannel = this.getOpenerEventChannel()
	// 监听 acceptDataFromOpenerPage 事件，获取上一页面通过 eventChannel 传送到当前页面的数据
	eventChannel.on('addHealthNote', function(data) {
	  console.log('child addHealthNote',data)
	  if(data.personal){
		  that.updatedrinking(that,data.personal);
	  }
	  that.setData({
		healthData: data
	  });
	})
  },
  updatedrinking(that,data){
	if(data.drinking.hasDrinking == 'yes'){
		  that.setData({
			  drinking:'饮酒'+data.drinking.years
		  })
	}else{
	  that.setData({
		drinking:'不饮酒'
	  })
	}
	if(data.smoking.hasSmoking == 'yes'){
	  that.setData({
		  smoking:'吸烟'+data.smoking.years
	  })
	}else{
	  that.setData({
		smoking:'不吸烟'
	  })
	}  
  },
// 新增家族史
  addFamilyHistory() {
    wx.navigateTo({
      url: '/vp_pz/pages/familydoctor/editfamily?type=add',
      events: {
        // 接收新增返回的数据
        addFamilyNote: (data) => {
          const newFamily = JSON.parse(data);
		  console.log(newFamily.hasFamilyHistory)
		  console.log(newFamily.customDiseases)
		  
		  this.setData({
		    'healthData.family': newFamily
		  });
		  const eventChannel = this.getOpenerEventChannel()
		  eventChannel.emit('addHealthNote', this.data.healthData);
        }
      }
    });
  },


  // 编辑各部分
  editSection: function(e) {
    const section = e.currentTarget.dataset.section;
	let url = ''
	if(section == 'past'){
		url = `/vp_pz/pages/familydoctor/editjiwang`
	}else{
		url = `/vp_pz/pages/familydoctor/editperson`
	}
    wx.navigateTo({
      url: url,
      events: {
        // 接收编辑页面返回的数据
        sendData: (data) => {
			let resdata = JSON.parse(data)
			this.setData({
			  [`healthData.${section}`]: resdata
			});
			if(section == 'personal'){
				this.updatedrinking(this,resdata);
			}
			console.log('get data',resdata)
			console.log(this.data.healthData)
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.emit('addHealthNote', this.data.healthData);
          
        }
      },
      success: (res) => {
        // 将当前数据传递给编辑页面
        res.eventChannel.emit('sendData', {
          data: this.data.healthData[section],
          section: section
        });
      }
    });
  },
});